<template>
  <el-form :model="form" class="form" label-width="80px" label-position="right">

    <el-form-item label=" 名称">
      <el-input v-model="form.name" :disabled="popData.type === 2" placeholder="请填写 名称" />
    </el-form-item>
    <el-form-item label=" 描述">
      <el-input
        v-model="form.description"
        :disabled="popData.type === 2"
        autosize
        :row="{min:2,max:10}"
        :maxlength="200"
        type="textarea"
        placeholder="请填写 描述" />
    </el-form-item>
    <el-form-item label=" 是否公开">
      <el-select v-model="form.publicFlag" :disabled="popData.type === 2">
        <el-option label="隐私" :value="false" />
        <el-option label="公开" :value="true" />
      </el-select>
    </el-form-item>
    <el-row>
      <el-col :span="6">
        <el-form-item>
          <el-button type="primary" @click="edit">{{ loadingText }}</el-button>
        </el-form-item>
      </el-col>
      <el-col :span="6">
        <el-form-item>
          <router-link :to="`/favorite/index`" style="margin-right: 5px">
            <el-button type="primary">取消</el-button>
          </router-link>
        </el-form-item>
      </el-col>
    </el-row>
  </el-form>
</template>
<script>
import favorite from '@/api/favorite'
export default {
  props: ['popData'],
  data() {
    return {
      form: {
        id: ''
      },
      loadingText: '保存'
    }
  },
  created() {
    const id = this.popData.id
    if (id) {
      this.form.id = id
      this.loadingText = '编辑'
      this.getDetailById(id)
    } else {
      this.loadingText = '保存'
    }
  },
  methods: {
    getDetailById(id) {
      favorite.getById(id).then(res => {
        this.form = res.data
      })
    },
    edit() {
      favorite.edit(this.form).then(res => {
        this.$message.success(res.message)
        this.$router.push('/favorite/index')
      })
    }
  }
}
</script>
<style>
.form{
  margin: auto auto;
}
</style>
